package com.swl.service.impl;

import com.swl.dao.TopicDao;
import com.swl.pojo.Topic;
import com.swl.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicDao topicDao;
    @Override
    public Integer insertTopic(Topic topic) {
        System.out.println("TopicServiceImpl执行了");
        return topicDao.insertTopic(topic);
    }

    @Override
    public Topic selectTopicByTid(int tid) {
        return topicDao.selectTopicByTid(tid);
    }

    @Override
    public List<Topic> selectAllTopic() {
        return topicDao.selectAllTopic();
    }

    @Override
    public List<Topic> selectTopicBySchoolIdAndCourseId(int schoolId, int courseId) {
        List<Topic> topics = topicDao.selectTopicBySchoolIdAndCourseId(schoolId, courseId);
        Random r=new Random();
        Set<Topic> hashSet=new HashSet<>(10);
       while (true){
           if (hashSet.size()>=5){
               break;
           }
           int random = r.nextInt(topics.size());
           hashSet.add(topics.get(random));
       }
        ArrayList<Topic> list = new ArrayList<>();
        for (Topic t:hashSet
             ) {
            list.add(t);
        }
        System.out.println("有多少个随机出的题目："+list.size());
        return list;
    }

    @Override
    public List<Topic> selectTopicBySchoolIdAndCourseIdAndDiff(int schoolId, int courseId, String diff) {
        List<Topic> topics = topicDao.selectTopicBySchoolIdAndCourseIdAndDiff(schoolId, courseId,diff);
        Random r=new Random();
        Set<Topic> hashSet=new HashSet<>();
        while (true){
            if (hashSet.size()>=5){
                break;
            }
            int random = r.nextInt(topics.size());
            hashSet.add(topics.get(random));
        }
        ArrayList<Topic> list = new ArrayList<>();
        for (Topic t:hashSet
        ) {
            list.add(t);
        }
        System.out.println("有多少个随机出的题目："+list.size());
        return list;
    }
}
